package org.devgframwork.flowsystem.api.admin.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.devgframwork.flowsystem.api.admin.menu.dao.MenuMapper;
import org.devgframwork.flowsystem.api.admin.menu.dao.RoleMenuMapper;
import org.devgframwork.flowsystem.api.admin.menu.entity.po.Menu;
import org.devgframwork.flowsystem.api.admin.menu.entity.po.RoleMenu;
import org.devgframwork.flowsystem.api.admin.menu.entity.vo.MenuVo;
import org.devgframwork.flowsystem.api.admin.menu.service.MenuService;
import org.devgframwork.flowsystem.api.admin.user.service.UserService;
import org.devgframwork.platform.model.ResultModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private UserService userService;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public ResultModel<List<MenuVo>> getMenuList() {
        List<Long> roleList = userService.getUserRoleIds();
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ROLE_ID", roleList);
        queryWrapper.eq("IS_DELETE", 0);
        List<RoleMenu> roleMenuList = roleMenuMapper.selectList(queryWrapper);
        List<Menu> menuList = new ArrayList<>();
        if (null != roleMenuList && roleMenuList.size() > 0) {
            List<Long> menuIds = roleMenuList.stream().map(e -> e.getMenuId()).distinct().collect(Collectors.toList());
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.in("MENU_ID", menuIds);
            menuQueryWrapper.eq("IS_DELETE", 0);
            menuList = menuMapper.selectList(menuQueryWrapper);
        }
        List<MenuVo> menuVoList = getMenuVoList(menuList, null, null);
        return ResultModel.newSuccess(menuVoList);
    }


    @Override
    public List<MenuVo> getMenuVoList(List<Menu> list, Long parentId, Integer filterMenuLevel) {
        List<MenuVo> menuVoList = new ArrayList<>();
        if (null == list || list.size() <= 0) {
            return menuVoList;
        }
        if (null == filterMenuLevel || filterMenuLevel.intValue() <= 0) {
            filterMenuLevel = 1;
        }
        Integer finalTempMenuLevel = filterMenuLevel;
        List<Menu> menuList = list.stream().filter(e -> e.getMenuLevel().equals(finalTempMenuLevel)).collect(Collectors.toList());
        for (Menu menu : menuList) {
            Long menuId = menu.getMenuId();
            Long parentMenuId = menu.getParentMenuId();
            Integer menuLevel = menu.getMenuLevel();
            if ((null == parentId && menuLevel.equals(1)) || (null != parentId && parentId.equals(parentMenuId))) {
                List<MenuVo> subMenuList = getMenuVoList(list, menuId, menuLevel + 1);
                menuVoList.add(MenuVo.getMenuVo(menu, subMenuList));
//                MenuVo menuVo = new MenuVo();
//                BeanUtils.copyProperties(menu, menuVo);
//                menuVo.setChildData(getMenuVoList(list, menuId, menuLevel + 1));
//               .......
//                menuVo.setIndex(menu.getMenuPath());
//                menuVo.setTitle(menu.getMenuName());
//                menuVo.setIcon(menu.getMenuIcon());
//                menuVo.setSubs(subMenuList.size() <= 0 ? null : subMenuList);
            }
        }
        return menuVoList;

    }
}
